﻿
@{
    ViewBag.Title = "RoleEdit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}


@model CGT.Infrastructure.Entity.GovRole

@section style{
    <style>
        .form-horizontal .control-label {
            width: 20%;
            float: left;
            text-align: right;
        }

        .col-sm-3 {
            width: 80%;
            float: left;
        }
    </style>
}
<form id="form1" class="form-horizontal">
    <section class="content">
        <div class="row">
            <div class="col-md-12">
                <div class="box-body">
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">分类名称：</label>
                        <div class="col-sm-3">
                            <select class="form-control" style="width:300px;" name="selRoleType" id="selRoleType"></select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-2 control-label" for="name">角色名称：</label>
                        <div class="col-sm-3">
                            <input type="text" class="form-control" value="@Model.RoleName" style="width:300px;" name="roleName" maxlength="50" id="roleName" />
                        </div>
                    </div>
                </div>
                <div class="box-footer text-center">
                    <button class="btn btn-primary" type="button" id="btnSave">确 定 </button>
                    <button class="btn" type="button" id="cancel">取 消 </button>
                    <input type="hidden" id="roleID" name="roleID" value="@Model.RoleID" />
                </div>
            </div>

        </div>
        <!-- /.row -->
    </section>

</form>
@section scriptsBottom{
    <script type="text/javascript">
       $(document).ready(function () {
           initFormValidate();
           initRoleType();
           $("#btnSave").click(function(){
               save();
           });
           $("#cancel").click(function () {
               BMCW.closeTopLayer();
           });
       });
       function initFormValidate() {
           $("#form1").bootstrapValidator({
               submitHandler: function (valiadtor, loginForm, submitButton) {
                   valiadtor.defaultSubmit();
               },
               fields: {
                   selRoleType: {
                       validators: {
                           notEmpty: {
                               message: '请选择分类'
                           }
                       }
                   },
                   roleName: {
                       validators: {
                           notEmpty: {
                               message: '名称不能为空'
                           },
                           stringLength: {
                               /*长度提示*/
                               min: 1,
                               max: 50,
                               message: '名称长度必须在1到50之间'
                           },
                           callback: {
                               message: '名称重复',
                               callback: function (value, validator, $field) {
                                   var bol = true;
                                   $.ajax({
                                       url: '@Url.Action("IsExistRoleName", "Authority")',
                                       type: 'GET',
                                       async: false,
                                       dataType: "json",
                                       data: {
                                           name: value,
                                           mid: @Model.RoleID,
                                       }

                                   }).done(function (data) {
                                       //刷新
                                       bol = data.status==-1?true:false;

                                   }).fail(function (data) {
                                       bol = true;
                                   });
                                   return bol;
                               }
                           }
                       }
                   }
               }
           });
       }

       function save()
       {
           var bootstrapValidator = $("#form1").data('bootstrapValidator');
           bootstrapValidator.validate();

           if (bootstrapValidator.isValid()) {
               var arrData = $("#form1").serializeArray();
               var data = {};
               for (var i = 0; i < arrData.length; i++) {
                   data[arrData[i].name] = arrData[i].value;
               }
               var url = '@Url.Action("SaveRole", "Authority")'
               BMCW.ajax({
                   url: url,
                   type: "POST",
                   data: data,
                   success: function (o) {
                       if (o.status == 0) {
                           BMCW.msg("保存成功",function(){
                               BMCW.getTopSelectedWin().search();
                           });
                       } else {
                           BMCW.alert("保存失败");
                       }
                   }
               });
           }
       }

       //角色分类
       function initRoleType()
       {
           var mid = @Model.RoleTypeID;
           $("#selRoleType").append("<option value=''>请选择角色分类</option>");
           var url = '@Url.Action("GetRoleTypeList", "Authority")'
           BMCW.ajax({
               url: url,
               type: "GET",
               success: function (o) {
                   if (o.status == 0) {
                       $.each(o.data, function (i) {
                           var sel = "";
                           if(o.data[i].RoleTypeID == mid)
                           {
                               sel = "selected='true'";
                           }
                           $("#selRoleType").append("<option value='" + o.data[i].RoleTypeID + "' " + sel + ">" + o.data[i].TypeName + "</option>");
                       });
                   }
               }
           });
       }
    </script>
}

